package com.isaiah.stack;

import com.isaiah.list.ArrayList;

/**
 * @Title: 顺序栈-基于数组的实现
 * @Description: ${todo}
 * @author: Haijun
 * @emial: isaiah@sohu.com
 * @date 2020/4/815:49
 */
public class ArrayStack<E> implements Stack<E> {

    public ArrayList<E> array;

    private ArrayStack(int capacity){
        array = new ArrayList(capacity);
    }

    public ArrayStack(){
        array = new ArrayList();
    }

    @Override
    public int getSize() {
        return this.array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return this.array.isEmpty();
    }
    // 获取容量，该方法只有基于数组实现的栈才有些方法
    public int getCapacity(){
        return this.array.getCapacity();
    }
    @Override
    public void push(E e) {
        this.array.addLast(e);
    }

    @Override
    public E pop() {
        return this.array.removeLast();
    }

    @Override
    public E peek() {
        return this.array.getLast();
    }

    @Override
    public String toString() {
        StringBuffer rs = new StringBuffer();
        rs.append(String.format("Stack: size = %d, capacity = %d\n", this.array.getSize(), this.array.getCapacity()));
        rs.append("[");
        for (int i = 0; i < this.array.getSize(); i ++){
            rs.append(this.array.get(i));
            if (i != this.array.getSize() -1){
                rs.append(", ");
            }
        }
        rs.append("] Top");
        return rs.toString();
    }
}
